#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int n1 = s.size(), n2 = p.size();
        int left = 0, right = 0, len = n2;
        unordered_map<int, int> hash1, hash2;
        vector<int> v;
        while (len)
        {
            hash2[p[--len]]++;
            hash1[s[right++]]++;
        }
        while (right < n1)
        {
            while (right < n1 && hash1 == hash2)
            {
                v.push_back(left);
                hash1[s[right++]]++;
                hash1[s[left]]--;
                if (hash1[s[left]] == 0) hash1.erase(s[left]);
                left++;
            }
            if (right < n1)
            {
                hash1[s[right++]]++;
                hash1[s[left]]--;
                if (hash1[s[left]] == 0) hash1.erase(s[left]);
                left++;
            }
        }
        if (hash1 == hash2) v.push_back(left);
        return v;
    }
};